package com.ali.stock.controller;

import com.ali.stock.config.WebSocketServer;
import com.ali.stock.config.WebSocketServer2;
import com.ali.stock.domain.SysDeal;
import com.ali.stock.domain.SysEntrust;
import com.ali.stock.service.SysDealService;
import com.ali.stock.service.SysEntrustService;
import com.ali.stock.service.SysEntrustUserService;
import com.ali.stock.service.SysUserDealService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@EnableScheduling
public class TestServiceImpl {

    @Autowired
    SysEntrustService sysEntrustService;

    @Autowired
    SysEntrustUserService sysEntrustUserService;

    @Autowired
    SysDealService sysDealService;

    @Autowired
    SysUserDealService sysUserDealService;


    /**
     * 将实时时间和数据传送到前端
     */
    @Scheduled(fixedRate = 1000) //1000毫秒执行一次
    public void printTime() {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(new Date());
        map.put("time", date);
        Integer userId = 1;
//        List<SysEntrust> todayEntrustList = sysEntrustService.getTodayEntrustList(Long.valueOf(userId));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("entrust_status", "已报");
        queryWrapper.eq("del_flag", 0);
        List<SysEntrust> entrustList = sysEntrustService.list(queryWrapper);
        List<SysEntrust> buyEntrustList = new ArrayList<>();
        List<SysEntrust> sellEntrustList = new ArrayList<>();
        for (SysEntrust sysEntrust : entrustList) {
            if (sysEntrust.getEntrustDirection().equals("买入")) {
                buyEntrustList.add(sysEntrust);
            } else {
                sellEntrustList.add(sysEntrust);
            }
        }
        map.put("buyEntrustList", buyEntrustList);
        map.put("sellEntrustList", sellEntrustList);
        WebSocketServer.sendInElse(map, userId + "");
        WebSocketServer2.sendInElse(map, 4 + "");
        System.out.println(date);
    }

    /**
     * 实现撮合交易
     */
//    @Scheduled(fixedDelay = 10000)
    public void delEentrust() {
        //先获取状态为已报的数据，并且未被删除（逻辑）
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("entrust_status", "已报");
        queryWrapper.eq("del_flag", 0);
        List<SysEntrust> sysEntrustList = sysEntrustService.list(queryWrapper);
        List<SysEntrust> buyEntrustList = new ArrayList<>();
        List<SysEntrust> sellEntrustList = new ArrayList<>();
        for (SysEntrust sysEntrust : sysEntrustList) {
            if (sysEntrust.getEntrustDirection().equals("买入")) {
                buyEntrustList.add(sysEntrust);
            } else {
                sellEntrustList.add(sysEntrust);
            }
        }
        //将同一个人出售或者求购的同一种股票，价格相同的，进行合并
        //并且数据关系中的数据也要相应的改变

    }

//    /**
//     * 添加委任时的逻辑
//     */
//    public void saveEntustTest(SysEntrust sysEntrustInfo) {
//        //先获取状态为已报的数据，并且未被删除（逻辑）
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("entrust_status", "已报");
//        queryWrapper.eq("del_flag", 0);
//        List<SysEntrust> sysEntrustList = sysEntrustService.list(queryWrapper);
//        List<SysEntrust> buyEntrustList = new ArrayList<>();
//        List<SysEntrust> sellEntrustList = new ArrayList<>();
//        for (SysEntrust sysEntrust : sysEntrustList) {
//            if (sysEntrust.getEntrustDirection().equals("买入")) {
//                buyEntrustList.add(sysEntrust);
//            } else {
//                sellEntrustList.add(sysEntrust);
//            }
//        }
//        //添加的时候判断添加成功与否
//        if (sysEntrustInfo.getEntrustDirection().equals("买入")) {
//            SysDeal sysDeal = new SysDeal();
//            //当交易成功时，对卖出的列表的影响
//
//            //没有交易成功时
//            if (sysDeal.getStoclName() == null) {
//
//                //新增的买入的委托单对买入的列表影响
//                for (SysEntrust sysEntrust : buyEntrustList) {
//                    //当要买入的股票的价格和类型在售卖的列表中存在的话
//                    if (sysEntrust.getEntrustStockCode().equals(sysEntrustInfo.getEntrustStockCode()) &&
//                            sysEntrust.getEntrustPrice().equals(sysEntrustInfo.getEntrustPrice())) {
//                        sysEntrust.setEntrustNum(sysEntrust.getEntrustNum() + sysEntrustInfo.getEntrustNum());
//                        sysEntrust.setUpdateTime(new Date());
//                        sysEntrustService.updateById(sysEntrust);
//                    } else {
//                        sysEntrustInfo.setEntrustTime(new Date());
//                        sysEntrustService.save(sysEntrustInfo);
//                    }
//                }
//
//            }
//        } else {
//            for (SysEntrust sysEntrust : sellEntrustList) {
//
//            }
//        }
//
//    }

}